from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep

import sys

sys.path.append(r'..')
from web_base.base_page import BasePage
from page_object.login_page import LoginPage


class AssetPage(BasePage):
    url = r'/bsams/front/asset/asset_list.do'
# 新增定位
    add_btn_loc = (By.CLASS_NAME, 'button_true')  # 点击新增
    add_title_loc = (By.ID, 'title')  # 输入内容
    add_providerId_loc = (By.ID, 'providerId')  # 选择地点
    add_storageId_loc = (By.ID, 'storageId')  # 供应商
    add_ok_loc = (By.CLASS_NAME, 'button_fujian.margin')  # 点击确定
# 查询定位
    query_btn_loc = (By.ID, 'status')  # 选择下拉框内容
    query_title_loc = (By.ID, 'title')  # 输入内容
    query_ok_loc = (By.CLASS_NAME, 'button_fujian.margin')  # 点击搜索
# 修改定位
    modify_btn_loc = (By.XPATH,'/html/body/div[2]/div/div[2]/div[2]/div[3]/table/tbody/tr[2]/td[8]/a[1]')
    modify_title_loc = (By.ID,'title')
    modify_storageId_loc = (By.ID, 'storageId')
    modify_providerId_loc = (By.ID, 'providerId')
    modify_ok_loc = (By.CLASS_NAME,'button_fujian.margin')
# 借用
    lend_btn_loc = (By.CLASS_NAME, 'padding5')
    lend_departId_loc = (By.ID, 'departId')
    lend_remark_loc = (By.ID, 'remark')
    lend_ok_loc = (By.CLASS_NAME, 'button_fujian.margin')
# 归还
    return_btn_loc = (By.CLASS_NAME,'return')
    return_ok_loc = (By.CLASS_NAME,'button_fujian.margin')
    def asset_add(self, title, providerId, storageId):
        self.open()
        self.ele_click(self.add_btn_loc)
        self.input_ele(self.add_title_loc, title)
        self.ele_select(self.add_providerId_loc, providerId)
        self.ele_select(self.add_storageId_loc, storageId)
        self.ele_click(self.add_ok_loc)
        sleep(3)
        text = self.get_alert_text()
        self.alert_click()
        return text

    def asset_modify(self,title, storageId, providerId):
        self.open()
        self.ele_click(self.modify_btn_loc)
        self.input_ele(self.modify_title_loc,title)
        self.ele_select(self.modify_providerId_loc, providerId)
        self.ele_select(self.modify_storageId_loc,storageId)
        self.ele_click(self.modify_ok_loc)
        sleep(3)
        text = self.get_alert_text()
        self.alert_click()
        return text

    # 资产查询
    def asset_query(self, status, title):
        self.open()
        self.ele_select(self.query_btn_loc, status)
        self.input_ele(self.query_title_loc, title)
        self.ele.click(self.query_ok_loc)
        sleep(2)
        text = self.get_alert_text()
        self.alert_click()
        return text

    def asset_lend(self,departId,remark):
        self.open()
        self.ele_click(self.lend_btn_loc)
        self.ele_select(self.lend_departId_loc,departId)
        self.input_ele(self.lend_remark_loc,remark)
        self.ele_click(self.lend_ok_loc)
        sleep(2)
        text = self.get_alert_text()
        self.alert_click()
        return text

    def asset_return(self):
        self.open()
        self.ele_click(self.return_btn_loc)
        self.ele_click(self.return_ok_loc)
        text = self.get_alert_text()
        self.alert_click()
        return text



if __name__ == '__main__':
    taskid = 1
    username = 'student'
    password = '123456'
    vericode = 'shtd'
    title = '联想小新01'
    providerId = '维信科技发展有限公司'
    storageId = '电脑设备库'

    chrome_location = r'E:\test\chrome-win\chrome.exe'
    options = webdriver.ChromeOptions()
    options.binary_location = chrome_location

    driver = webdriver.Chrome(options=options)

    login_page = LoginPage(driver)
    login_page.login(taskid, username, password, vericode)

    asset_page = AssetPage(driver)
    text = asset_page.asset_add(title, providerId, storageId)
    print(text)

    driver.quit()
